<?php
/**
 * Created by PhpStorm.
 * User: moko1988
 * Date: 16/01/016
 * Time: 下午4:58
 */

namespace Admin\Controller;
use Think\Controller;

class PictureController extends Controller {


    /*
     * 列表
     */
    public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');
        $page_size = I('post.page_size/d');
        $cid = I('post.cid');

        $where = array();

        if($cid != ''){
            $where['cid'] = $cid;
        }

        $PictureModel = M('Picture');

        $count = $PictureModel->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $result = $PictureModel->where($where)->order('rank desc')->limit($limitStart,$page_size)->select();

        if($result !== false){

            $data['page']['total_page'] = $total;

            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*保存*/
    public function save(){

        $this->isAjaxLogin();//检查是否登录

        $PictureModel = M('Picture');

        $id     = I('post.id/d');
        $name   = I('post.name');
        $cid    = I('post.cid');
        $url    = I('post.url');
        $litpic = I('post.litpic');

        $data['name']   = $name;
        $data['cid']   = $cid;
        $data['url']   = $url;
        $data['litpic'] = $litpic;

        /*修改*/
        if($id){

            $result = $PictureModel->where('id='.$id)->save($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
            }

        }
        /*添加*/
        else{

            $data['rank']  = $PictureModel->count() + 1;

            $lastid = $PictureModel->add($data);

            if($lastid !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据添加失败，请稍后在试！";
            }
        }

        $this->ajaxReturn($ajaxReturn);

    }

    /*
     * 详情
     */
    public function infos (){

        $this->isAjaxLogin();

        $id = I('post.id/d');

        $PictureModel = M('Picture');

        $result = $PictureModel->where('id='.$id)->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*删除*/
    public function delete(){

        $this->isAjaxLogin();//检查是否登录

        $id = I('post.id/d');

        $PictureModel = M('Picture');

        $PictureModel->where('id='.$id)->delete();

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }


    /*
     * 排序
     */
    public function rank (){

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $PictureModel = M('Picture');

        $thisinfo   = $PictureModel->where('id='.$thisid)->getField('rank,cid');
        $thisrank   = array_keys($thisinfo)[0];
        $thiscid    = array_values($thisinfo)[0];


        $nextinfo   = $PictureModel->where('rank'.$and.$thisrank.' AND cid='.$thiscid)->order('rank '.$order)->getField('id,rank');
        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        if($thisrank && $nextid && $nextrank){

            $PictureModel-> where('id='.$thisid)->setField('rank',$nextrank);
            $PictureModel-> where('id='.$nextid)->setField('rank',$thisrank);

            $data['thisid'] = $thisid;
            $data['thisrank'] = $thisrank;
            $data['nextid'] = $nextid;
            $data['nextrank'] = $nextrank;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }


}